/* tipo -> 0 = registrar
		   1 = modifica */
function registro_perso(tipo) {
	var fun = '';
		if(tipo == '0')
			fun = 'regper';
		else
			fun = 'modper';
		
	var id = $("#id_oculto").val();
	var t_doc = $("#cbxtdoc").val();
	var n_doc = $("#txtndoc").val();
	var ape_pat= $("#txtapat").val().toUpperCase();
	//var ape_mat= $("#txtapat").val().toUpperCase();
	//var nom = $("#txtapat").val().toUpperCase();
	var fnac = $("#txtfnac").val();
	var dir = $("#txtdir").val();
	var cargo = $("#cbx_cargo").val();
	var ruta = $("#ruta").val();	
	var tel = $("#txttel").val();
		if (ruta == '')
			ruta = urljs + 'images/foto_persona.jpg';
	var imagen=urljs+"images/loading.gif";
	if(n_doc == '')
		{
		alert("Verifique el numero de su documento");
		}
	else if(fnac.length == 0 || fnac.length !=10)
		{
		alert("Verifique su fecha de Nacimiento");	
		}
	else if(dir == '' )
		{
		alert("Verifique su Direccion");
		}
	else if(cargo < 1 || cargo > 1000000)
	{
	alert("Verifique su cargo");
	}
	else
		{
		var sexo = "";
	     elementos = document.getElementById('formu').elements;
	     longitud = document.getElementById('formu').length;
	     for (var i = 0; i < longitud; i++){
	         if(elementos[i].name == 's' && elementos[i].type == "radio" 
	        	&& elementos[i].checked == true){
	        	 sexo = elementos[i].value;
	         }
	     }
		$.ajax({
			datatype : "html",
			type: "POST",
			url: urljs + "index.php/Ruos/" + fun,
			data: "id= " + id +
				  "&t_doc= " + t_doc +
				  "&n_doc= " + n_doc +
				  "&ape_pat=" + ape_pat+
				  "&ape_mat=" + ape_pat+
				 "&nom=" + ape_pat+
				  "&fnac=" + fnac+
				  "&sexo=" + sexo+
				  "&dir=" + dir+
				  "&cargo="+ cargo+ 
				  "&ruta=" + ruta+
				  "&tel=" + tel,
				  
			beforeSend: function(){
				$("#divcarga").show();				
				$("#divcarga").html("<p style='margin-top:50;'><center>" +
									"<h3>Procesando</h3><img src="+imagen+">" +
									"</center></p>");
			},
			success: function(requestData) {
				$("#divcarga").html(requestData);				
				location.reload();
			},
			error: function(requestData, strError, strTipoError) {
			    $("#divcarga").html("Error: Disculpe las molestias intente cargar de nuevo." + 
			    					Error);
			} 
		});  
		}
}

function carga_datos_repre(){
	//'. $info[0][6] .' id
	
	var id = $('#cbxorg_soc').val();
	$.ajax({
		datatype : "html",
		type: "POST",
		url: urljs + "index.php/Ruos/carga_datos_repre",
		data: "id=" + id,
			  
		beforeSend: function(){
			/*$("#divcarga").show();				
			$("#divcarga").html("<p style='margin-top:50;'><center>" +
								"<h3>Procesando</h3><img src="+imagen+">" +
								"</center></p>");*/
		},
		success: function(requestData) {
			$("#divcarga").html(requestData);	
		},
		error: function(requestData, strError, strTipoError) {
		    $("#divcarga").html("Error: Disculpe las molestias intente " +
		    		"cargar de nuevo." + 
		    					Error);
		} 
	}); 
}

function PersonaLista(page_index, jq){
	var items_per_page = $("#items_per_page").val();
	var max_elem = Math.min((page_index+1) * items_per_page, members.length);
	var newcontent = "";

	newcontent =  '<table class="tabla">';
	newcontent += '<thead>';
	newcontent += '<tr>';
	newcontent += '<th width="50px">ID.</th>';
	newcontent += '<th width="500px">Apellidos Y Nombres</th>';
	newcontent += '<th width="100px">Documento </th>';
	newcontent += '<th width="100px">Nro. de Documento</th>';
	/*newcontent += '<th width="100px">Cargo</th>'; */
	newcontent += '</tr>';
	newcontent += '</thead>';
	
	for(var i=page_index * items_per_page; i<max_elem; i++){
		varbtn = "";
		
		if(members[i][0] > 0)
		{
			//pintada = "#EEE8AA";
			varbtnupd = "<button id='btn' value='" + members[i][0] + "'type='button'" +
					" class='btneditar'"+ 
			"onClick='modificar_persona(this.value,\""+members[i][1]+"\");'"+"></button>";
			varbtndel = "<button id='btn' value='" + members[i][0] + "'type='button' class='btneliminar'"+ 
			"onClick='eliminar_persona(this.value,\""+members[i][1]+"\");'"+"></button>";
			varbtnrepor = "<button id='btn' value='" + members[i][0] + "'type='button' class='btninfo'"+ 
			"onClick='reporte_persona(this.value,\""+members[i][1]+"\");'"+"></button>";
			
		}
			
		var cadcontentrow = "<tr>";
		cadcontentrow += '<td align="center" width="50px">'+ members[i][0] +'</td>';
		cadcontentrow += '<td>' + members[i][1] + '</td>';
		cadcontentrow += '<td>' + members[i][2] + '</td>';
		cadcontentrow += '<td>' + members[i][3] + '</td>';
		/*cadcontentrow += '<td>' + members[i][4] + '</td>'; */
		cadcontentrow +='<td align="center" width="30px">'+ varbtnrepor +'</td>';
		cadcontentrow +='<td align="center" width="30px">'+ varbtnupd +'</td>';
		cadcontentrow +='<td align="center" width="30px">'+ varbtndel +'</td>';
		cadcontentrow += '</tr>';

		newcontent += cadcontentrow;
	}

	newcontent += "</table>";

	$("#locategridresult").html(newcontent);
	$("#txtbusper").focus();
	return false;
}

//para ver carnets
function PersonaCarnet(page_index, jq){
	var items_per_page = $("#items_per_page").val();
	var max_elem = Math.min((page_index+1) * items_per_page, members.length);
	var newcontent = "";

	newcontent =  '<table class="tabla">';
	newcontent += '<thead>';
	newcontent += '<tr>';
	newcontent += '<th width="50px">IDOS</th>';
	newcontent += '<th width="250px">Organizaci&oacute;n Social</th>';
	newcontent += '<th width="250px">Representate</th>';
	newcontent += '<th width="100px">Documento </th>';
	newcontent += '<th width="100px">Nro. de Documento</th>';	
	newcontent += '</tr>';
	newcontent += '</thead>';
	
	for(var i=page_index * items_per_page; i<max_elem; i++){
		varbtn = "";
		
		if(members[i][0] > 0)
		{
			//pintada = "#EEE8AA";			
			varbtnrepor = "<button id='btn' value='" + members[i][0] + "'type='button' class='btninfo'"+ 
			"onClick='reporte_personacarnet(this.value,\""+members[i][1]+"\");'"+"></button>";
			
		}
			
		var cadcontentrow = "<tr>";
		cadcontentrow += '<td align="center" width="50px">'+ members[i][0] +'</td>';
		cadcontentrow += '<td>' + members[i][1] + '</td>';
		cadcontentrow += '<td>' + members[i][2] + '</td>';
		cadcontentrow += '<td>' + members[i][3] + '</td>';
		cadcontentrow += '<td>' + members[i][4] + '</td>';
		cadcontentrow +='<td align="center" width="30px">'+ varbtnrepor +'</td>';
		cadcontentrow += '</tr>';

		newcontent += cadcontentrow;
	}

	newcontent += "</table>";

	$("#locategridresult").html(newcontent);
	$("#txtbusper").focus();
	return false;
}


function eliminar_persona(cod,nom){

	if(confirm('Esta seguro de Eliminar a:? ' + nom))
		{		
			$.ajax({
				datatype : "html",
				type: "POST",
				url: urljs + "index.php/Ruos/eliminapersona",
				data: "idpersona= " + cod ,
					  
				beforeSend: function(){
					$("#divcarga").show();				
					$("#divcarga").html("<p style='margin-top:50;'><center>" +
										"<h3>Procesando</h3><img src="+imagen+">" +
										"</center></p>");
				},
				success: function(requestData) {
					$("#divcarga").html(requestData);
					location.reload();
				},
				error: function(requestData, strError, strTipoError) {
				    $("#divcarga").html("Error: Disculpe las molestias intente cargar de nuevo." + 
				    					Error);
				}
			});	
		}
	
}

function modificar_persona(cod,nom){
		location.href = urljs + 'index.php/Ruos/admper?id='+cod ;
}

function dar_rutaimg(){
	var file = document.getElementById('file_img');
	var r=file.value.substring(12,file.value.length);
		if( r == '')
			r= 'foto_persona.jpg';
	var url= urljs + "images/"+ r;
	//alert( url);
	document.getElementById('ruta').value = url;
	document.getElementById("div").innerHTML=
		'<img src="' + url +'" width="100" height="150" >';
}
//reportes
function reporte_persona(cod,nom)
{
	tipo="pdf";
    nombre="Persona_370";
	//openlight(urlreportjs+"tipo="+tipo+"&reporte="+nombre+"&param=idos^"+cod,300,500,false);
	//window.open(urlreportjs+"tipo="+tipo+"&reporte="+nombre+"&param=idper^"+cod,"_blank");
	window.open(urlreportjs+"tipo="+tipo+"&reporte="+nombre+"&param=idper^"+cod,"_blank");
}

function reporte_personacarnet(cod,nom)
{
	tipo="pdf";
	nombre="Carnet_370";	
	
	/*pagina=urlreportjs+"tipo=pdf&reporte=Carnet3";
	pagina +="&param=";
	pagina +="idos^"+cod+"|";
	window.open(pagina);*/
	//pagina +="&param=";	pagina +="p_user^"+user+"|";
	//urljs + 'index.php/Ruos/admper
	//openlight(urljs + 'index.php/Ruos/admper?',300,500,false);
	//window.open("http://172.17.1.124:8080/reportesbuild/index.jsp?"+"tipo="+tipo+"&reporte="+nombre+"&param=idos^"+cod);
	window.open(urlreportjs +"tipo="+tipo+"&reporte="+nombre+"&param=idos^"+cod);
}

//Busquedas
function buscar_persona(){
	var desc = $("#txtbusper").val();
	var tipo = $("#cbxtipo").val();
	var img = urljs + 'images/loading.gif';
		if(desc == '')
			//alert('Ingrese alguna descripcion');
			location.reload();
		else
		{
			$.ajax ({
				datatype: 'html',
				type: 'Post',
				url: urljs + 'index.php/Ruos/busqpers',
				data: "desc=" + desc +
					  "&tipo=" + tipo,
					  
				beforeSend: function(){					
					$("#locategridresult").show();				
					$("#locategridresult").html("<p style='margin-top:50;'><center>" +
										"<h3>Procesando</h3><img src="+img+">" +
										"</center></p>");
				},
				success: function(requestdata){
					$('#locategridresult').html(requestdata);
				},
				error: function(requestData, strError, strTipoError){
					$('#locategridresult').html('No se encuentra');									
				}
				
			});
		}
			
}

//busqueda de carnet
function buscar_persona_carnet(){
	var desc = $("#txtbusper").val();
	var tipo = $("#cbxtipo").val();
	var img = urljs + 'images/loading.gif';
		if(desc == '')
			//alert('Ingrese alguna descripcion');
			location.reload();
		else
		{
			$.ajax ({
				datatype: 'html',
				type: 'Post',
				url: urljs + 'index.php/Ruos/busqperscarnet',
				data: "desc=" + desc +
					  "&tipo=" + tipo,
					  
				beforeSend: function(){					
					$("#locategridresult").show();				
					$("#locategridresult").html("<p style='margin-top:50;'><center>" +
										"<h3>Procesando</h3><img src="+img+">" +
										"</center></p>");
				},
				success: function(requestdata){
					$('#locategridresult').html(requestdata);
				},
				error: function(requestData, strError, strTipoError){
					$('#locategridresult').html('No se encuentra');									
				}
				
			});
		}
			
}
